package com.bdqn.petmalldemo.controller;


import com.alibaba.fastjson.JSONObject;
import com.bdqn.petmalldemo.bo.ShippingAddBo;
import com.bdqn.petmalldemo.common.ResultCodeEnum;
import com.bdqn.petmalldemo.common.ReturnResult;
import com.bdqn.petmalldemo.common.TokenCache;
import com.bdqn.petmalldemo.entity.Shipping;
import com.bdqn.petmalldemo.entity.User;
import com.bdqn.petmalldemo.service.ShippingService;
import com.bdqn.petmalldemo.utils.CommonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lenovo
 * @since 2020-02-10
 */
@Api(tags = "用户收货信息控制器")
@RestController
@RequestMapping("/shipping")
public class ShippingController {
    @Autowired
    private ShippingService shippingService;

    @ApiOperation(value = "查询用户收货地址")
    @PostMapping(value = "/list")
    public ReturnResult getShippingList(@ApiParam(name = "token",value = "用户登录Token",required = true)
                                        String token){
        //先根据Token查询当前登录用户信息
        String jsonUser = TokenCache.get(token);
        //判断当前用户是否登录
        if (StringUtils.isEmpty(jsonUser)) {
            return ReturnResult.error().msg(ResultCodeEnum.USER_NOT_LOGIN.getMsg())
                    .code(ResultCodeEnum.USER_NOT_LOGIN.getCode());
        }
        User user = JSONObject.parseObject(jsonUser, User.class);
        //根据当前登录的用户Id查询收货信息
        List<Shipping> shippingList = shippingService.getShippingByUserId(user.getId());
        if (CollectionUtils.isEmpty(shippingList)) {
            return ReturnResult.error().code(ResultCodeEnum.SHIPPING_NULL.getCode())
                    .msg(ResultCodeEnum.SHIPPING_NULL.getMsg());
        }
        return ReturnResult.ok().data(shippingList);
    }
    @ApiOperation(value = "新增用户收货地址")
    @PostMapping(value = "/add")
    public ReturnResult addshippping(ShippingAddBo shippingAddBo){
        //先根据token查询当前登录用户信息
        String jsonUser = TokenCache.get(shippingAddBo.getToken());
        //判断当前用户是否登录
        if (StringUtils.isEmpty(jsonUser)) {
            return ReturnResult.error().msg(ResultCodeEnum.USER_NOT_LOGIN.getMsg())
                        .code(ResultCodeEnum.USER_NOT_LOGIN.getCode());
        }
        User user = JSONObject.parseObject(jsonUser, User.class);
        //根据当前登录的用户Id查询收货信息
        List<Shipping> shippingList = shippingService.getShippingByUserId(user.getId());
        //判断当前用户收货信息是否大于10
        if (shippingList.size()>10) {
            return ReturnResult.error().msg(ResultCodeEnum.SHIPPING_MORE_THAN_THE_SPECIFIED_AMOUNT.getMsg())
                    .code(ResultCodeEnum.SHIPPING_MORE_THAN_THE_SPECIFIED_AMOUNT.getCode());
        }
        shippingAddBo.setUserId(user.getId());
        shippingService.addShipping(shippingAddBo);
        return ReturnResult.ok();
    }

   @ApiOperation(value = "编辑收货地址")
    @PostMapping(value = "get")
    public ReturnResult getShippingById(String token,String shippingId){
       ReturnResult returnResult = CommonUtils.checkUserLogin(token);
       Shipping shipping = shippingService.getById(shippingId);
       if (shipping==null) {
          return ReturnResult.error().code(ResultCodeEnum.DATA_NULL.getCode())
                  .msg(ResultCodeEnum.DATA_NULL.getMsg());
       }
       return returnResult.data(shipping);
   }

   @ApiOperation(value = "修改收货地址")
   @PostMapping(value = "update")
   public ReturnResult updateById(String token,Shipping shipping){
       ReturnResult returnResult = CommonUtils.checkUserLogin(token);
        shippingService.updateById(shipping);
       return returnResult;
   }

   @ApiOperation(value = "删除")
    @PostMapping("delete")
    public ReturnResult deleteShippingById(String token,String shippingId){
       ReturnResult returnResult = CommonUtils.checkUserLogin(token);
       shippingService.deleteShipping(shippingId);
       return returnResult;
   }
}

